﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;

namespace RevisionHost
{
    public partial class RevisionUC : UserControl
    {
        public RevisionUC()
        {
            InitializeComponent();
        }

        private void buttonConceptFileBrowse_Click(object sender, EventArgs e)
        {
            Global.candidateList = new Dictionary<int, Dictionary<string, bool>>();
            openFileDialog_LoadPlugin.Multiselect = false;
            openFileDialog_LoadPlugin.FileName = string.Empty;
            //openFileDialog_LoadPlugin.Filter = "Plugin (*.dll) |*.dll";
            if (openFileDialog_LoadPlugin.ShowDialog() == DialogResult.OK)
            {
                this.textBoxConceptFilePath.Text = openFileDialog_LoadPlugin.FileName;
                Global.conceptList = Concept.FromConceptFile(this.textBoxConceptFilePath.Text);
                // make dictionary of candidate list of a concept and its chosen state.
                foreach (Concept curConcept in Global.conceptList)
                {
                    Dictionary<string, bool> curCandidatesList = new Dictionary<string, bool>();
                    foreach (string curCandidate in curConcept.Candidates)
                    {
                        curCandidatesList.Add(curCandidate, false);
                    }
                    Global.candidateList.Add(curConcept.ID, curCandidatesList);
                }

                List<Webpage> allWebpages = new List<Webpage>();
                try
                {
                    allWebpages = DAL.WebpageDAL.GetWebpages();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                // make webpage dictionary
                Dictionary<int, Webpage> allWebpagesDict = new Dictionary<int, Webpage>();
                foreach (Webpage curWebpage in allWebpages)
                {
                    allWebpagesDict.Add(curWebpage.ID, curWebpage);
                }
                // add concepts to treeview control
                this.treeViewConcept.Nodes.Clear();
                foreach (Concept curConcept in Global.conceptList)
                {
                    TreeNode curTreeNode = new TreeNode("Concept " + curConcept.ID + " [ " + curConcept.WebpageInstanceVector.Count + " instance(s) ]");
                    // calculate the feature vector
                    curTreeNode.ImageIndex = curConcept.ID;

                    string valueStr = curConcept.FeatureVector.ToString().Trim();
                    valueStr = valueStr.Replace(" ", " , ");
                    // add feature vector node
                    curTreeNode.Nodes.Add(new TreeNode("Feature Vector [ " + valueStr + " ] ", -1, -1));
                    // add instances node
                    TreeNode instancesTreeNode = new TreeNode("Instances [ " + curConcept.WebpageInstanceVector.Count + " ] ");
                    instancesTreeNode.ImageIndex = -1;
                    foreach (Vector curVector in curConcept.WebpageInstanceVector)
                    {
                        Webpage curWebpage = allWebpagesDict[curVector.WebpageID];
                        instancesTreeNode.Nodes.Add(new TreeNode(" [" + curWebpage.ID + "] [ " + curWebpage.Title + " ] [ " + curWebpage.URL + "]", -2, -2));
                    }

                    curTreeNode.Nodes.Add(instancesTreeNode);
                    this.treeViewConcept.Nodes.Add(curTreeNode);
                }
            }
        }

        private void buttonSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBoxConceptCandidates.Items.Count; i++)
            {
                checkedListBoxConceptCandidates.SetItemChecked(i, true);
            }
        }

        private void buttonSelectNone_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBoxConceptCandidates.Items.Count; i++)
            {
                checkedListBoxConceptCandidates.SetItemChecked(i, false);
            }
        }

        private void checkedListBoxConceptCandidates_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            string candidateText = checkedListBoxConceptCandidates.Items[e.Index].ToString();

            if (e.NewValue == CheckState.Checked)
                Global.candidateList[Global.curChosenConceptID][candidateText] = true;
            else
                Global.candidateList[Global.curChosenConceptID][candidateText] = false;
        }

        private void treeViewConcept_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.ImageIndex >= 0)
            {
                Global.curChosenConceptID = e.Node.ImageIndex;
                // add candidates of this concept to the checked list box control
                checkedListBoxConceptCandidates.Items.Clear();
                Dictionary<string, bool> curCandidateList = Global.candidateList[Global.curChosenConceptID];
                foreach (string curCandidate in curCandidateList.Keys.ToList())
                {
                    int index = checkedListBoxConceptCandidates.Items.Add(curCandidate);
                    checkedListBoxConceptCandidates.SetItemChecked(index, curCandidateList[curCandidate]);
                }
            }
        }

        private void buttonExport2XML_Click(object sender, EventArgs e)
        {
            // refine candidates list of concepts
            List<Concept> refinedConceptList = new List<Concept>();
            foreach (Concept curConcept in Global.conceptList)
            {
                Concept tempConcept = new Concept();
                tempConcept.ID = curConcept.ID;
                tempConcept.FeatureVector = curConcept.FeatureVector;
                tempConcept.WebpageInstanceVector = new List<Vector>(curConcept.WebpageInstanceVector);
                tempConcept.Candidates = new List<string>();
                foreach (string curCandidate in curConcept.Candidates)
                {
                    if (Global.candidateList[curConcept.ID][curCandidate] == true)
                        tempConcept.Candidates.Add(curCandidate);
                }
                refinedConceptList.Add(tempConcept);
            }

            FolderBrowserDialog myFolderBrowseDlg = new FolderBrowserDialog();
            myFolderBrowseDlg.Description = "Choose the directory that will contain the exported xml file.";
            if (myFolderBrowseDlg.ShowDialog() == DialogResult.OK)
            {
                List<Webpage> allWebpagesList = DAL.WebpageDAL.GetWebpages();
                string filePath = myFolderBrowseDlg.SelectedPath;
                filePath += @"\" + "concept_" + DateTime.Now.ToString("MMM-d-yyyy HH-mm-ss") + ".xml";
                Concept.ToConceptFile(refinedConceptList, filePath, allWebpagesList);
            }
        }
    }
}
